package com.yan_jiu_sheng.LeetCodeHot100.AC;

import java.util.HashMap;
import java.util.Stack;

/**
 * https://leetcode.cn/problems/valid-parentheses/?envType=study-plan-v2&envId=top-100-liked
 * AC 通过
 *
 * @author yulongTian
 * @create 2024-07-10 16:46
 */
public class Test67 {
    public static void main(String[] args) {
        System.out.println(new Test67().isValid("()[]{}"));
    }

    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        HashMap<Character, Character> map = new HashMap<>();
        map.put('(', ')');
        map.put('{', '}');
        map.put('[', ']');
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            // 入栈
            if (c == '(' || c == '{' || c == '[') {
                stack.push(c);
            } else {
                // 出栈
                if (stack.empty()) {
                    return false;
                } else {
                    Character pop = stack.pop();
                    if (c != map.get(pop)) {
                        return false;
                    }
                }
            }
        }
        return stack.empty();
    }

}
